// ==UserScript==
// @name           Hatena Group Diary Editor Extention
// @namespace      http://d.hatena.ne.jp/Meguu
// @include        http://*.g.hatena.ne.jp/*/edit*
// @version        0.5
// @lastmodified   July 11, 2007
// ==/UserScript==

(function () {
	
	var mydiaryurl = location.href.match(/http:\/\/[^\.]+\.g\.hatena\.ne\.jp\/[^\/]+\//);
	var daytextinput = document.evaluate(".//div[@class='day']/form/div[@class='body']/textarea", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	
	
	/* 
	 * Almost same as http://d.hatena.ne.jp/js/category.js, CategorySelecton.onChange
	 */
	function addCategory(cstr) {
		
		var caretPos = daytextinput.selectionStart;
		var originalText = daytextinput.value;
		var lineFeedCode = '\n';
		if (originalText.indexOf('\r\n')>=0) {
			lineFeedCode = '\r\n';
		}

		// move caretPos to end of the line
		var lineFeedIndex = originalText.indexOf(lineFeedCode, caretPos);
		if (lineFeedIndex >= 0) {
			caretPos = lineFeedIndex;
		} else {
			caretPos = originalText.length;
		}
		var newCategory = '['+cstr+']';
		var preText = daytextinput.value.substr(0, caretPos);
		var lines = preText.split(lineFeedCode);

		var categoryInsertIndex = 0;
		for (var i=lines.length-1; i>=0; i--) {
			var line = lines[i];
			if (line.match(/(^\*[^\*]|^\*$)/)) { // *~~ or * (not **)
				var match = line.match(/^(\*.+\*)(.*)/);
				var capital = '*';
				var topic = '';
				if (match && match.length>=3) { // *12345* or *t*
					capital = match[1];
					topic = match[2];
				} else { // *~~
					topic = line.substr(1);
				}
				if (i>0) {
					categoryInsertIndex += lines.slice(0, i).join(lineFeedCode).length;
					categoryInsertIndex += lineFeedCode.length;
				}
				if (topic.match(/^(\[.+\])(.*)/)) { // exist old category
					categoryInsertIndex += (capital + RegExp.$1).length;
				} else {
					categoryInsertIndex += capital.length;
				}
				break;
			}
		}
		if (categoryInsertIndex == 0) {
			newCategory = '*' + newCategory;
		}
		
		daytextinput.value = daytextinput.value.substring(0, categoryInsertIndex)+ newCategory + daytextinput.value.substring(categoryInsertIndex, daytextinput.value.length);
		daytextinput.selectionStart = categoryInsertIndex;
		daytextinput.selectionEnd = categoryInsertIndex + newCategory.length;
		daytextinput.focus();
	}
	
	function onSelectCategory(e) {
		addCategory(e.target.getAttribute("value"));
	}
	
	GM_xmlhttpRequest({
		method: "GET",
		url: mydiaryurl.toString(),
		onload: function(details) {
			var res = document.createElement('div');
			res.innerHTML = details.responseText;

			var categories = document.evaluate(".//ul[@class='hatena-sectioncategory']/li/a", res, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			
			var categorylist = document.createElement("select");
			for(var i=0; i<categories.snapshotLength; i++) {
				var category = document.createElement("option");
				var categoryname = categories.snapshotItem(i).innerHTML;
				category.setAttribute("value", categoryname);
				category.innerHTML = categoryname;
				category.addEventListener("click", onSelectCategory, true);
				categorylist.appendChild(category);
			}
			daytextinput.parentNode.insertBefore(categorylist, daytextinput);
			daytextinput.parentNode.insertBefore(document.createElement("br"), daytextinput);
			
		}
	});
	
})();
